package cn.kgc.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * @Author: 课工场
 * @Version: v1.0  2023/6/27
 * @Description:
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Autowired
    private RestTemplate  restTemplate;

    @RequestMapping("m1")
    public String method() {
        // 发送http请求  http://localhost:8503/order/m1
        RestTemplate restTemplate = new RestTemplate();
        String result = restTemplate.getForObject("http://" + getHost() + "/order/m1", String.class);
        return "userService ok => orderService => " + result;
    }


    // ribbon组件实现负载均衡   DiscoveryClient
    @RequestMapping("m2")
    public String method2() {

        /*
        *   http://192.168.253.1:8503
            http://192.168.253.1:8504
            http://192.168.253.1:8505
        * */
        List<ServiceInstance> orders = discoveryClient.getInstances("ORDERS");

        //for (ServiceInstance order : orders) {
        //    URI uri = order.getUri();
        //    System.out.println(uri);
        //}

        Random random = new Random();
        int index = random.nextInt(orders.size());

        ServiceInstance serviceInstance = orders.get(index);
        URI uri = serviceInstance.getUri();

        // 发送http请求  http://localhost:8503/order/m1
        RestTemplate restTemplate = new RestTemplate();
        String result = restTemplate.getForObject(uri+"/order/m1", String.class);
        return "userService ok => orderService => " + result;
    }


    @RequestMapping("m3")
    public String method3() {

        //  从注册中心获取一个可用通信节点  默认使用轮循策略 选择通信节点
        ServiceInstance orders = loadBalancerClient.choose("ORDERS");
        URI uri = orders.getUri();
        // 发送http请求  http://localhost:8503/order/m1
        RestTemplate restTemplate = new RestTemplate();
        String result = restTemplate.getForObject(uri+"/order/m1", String.class);
        return "userService ok => orderService => " + result;

    }


    @RequestMapping("m4")
    public String method4() {

        String result = restTemplate.getForObject("http://ORDERS/order/m1", String.class);
        return "userService ok => orderService => " + result;

    }



        //获取随机的主机节点
    public String getHost() {

        List<String> hosts = Arrays.asList("localhost:8503", "localhost:8504", "localhost:8505");
        Random random = new Random();
        int index = random.nextInt(hosts.size());
        return hosts.get(index);
    }
}
